﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Collections;
using System.Web.UI;
using System.Web.Security;
using ExtraClub.Domain.Entity;
using ExtraClub.Domain.Entities;
using SubSonic;

namespace ExtraClub.Domain.Framework {
    public interface ISiteContext {
        MembershipUser User { get; }
        string UserRole { get; }
        string Language { get; }
        WebPage Page { get; }
        BasePage BasePage { get; }
        VwClubProfile Club { get; }

        void SetBasePage(BasePage page);
        void SetWebPage();
        void SetLanguage();
        void SetLanguage(string lang);
        void SetClub();
    }
    internal class SiteContextInstance : ISiteContext {
        #region Fields
        private readonly HttpContext _httpContext;

        private string _language;
        private VwClubProfile _club;
        private WebPage _page;
        private BasePage _base_page;

        private HybridDictionary _items;
        private int _httpStatusCode = 200;
        private string _httpStatus = "OK";
        #endregion

        #region Properties
        public HttpContext Context { get { return _httpContext; } }
        /// <summary>
        /// Lets storing objects and fetch them from any place in the context (a one page life cycle)
        /// </summary>
        public IDictionary Items {
            [DebuggerStepThrough]
            get { return _items ?? (_items = new HybridDictionary()); }
        }

        public string Language {
            get {
                return _language;
            }
        }
        public VwClubProfile Club {
            get {
                if (_club == null) {
                    SetClub();
                }
                return _club;
            }
        }
        public BasePage BasePage {
            get {
                return _base_page;
            }
        }
        public WebPage Page {
            get {
                return _page;
            }
        }
        public string UserRole {
            get {
                string ret = null;

                if (_httpContext.User.Identity.IsAuthenticated) {
                    string[] roles = Roles.GetRolesForUser();
                    if (roles.Length > 0) {
                        ret = roles[0];
                    }
                }
                return ret;
            }
        }
        public MembershipUser User {
            get {
                MembershipUser ret = null;
                if (_httpContext.User.Identity.IsAuthenticated) {
                    ret = Membership.GetUser();
                }
                return ret;
            }
        }

        #endregion

        #region .ctor
        internal SiteContextInstance(HttpContext context) {
            this._httpContext = context;
        }
        #endregion

        #region Set Working objects
        public void SetBasePage(BasePage page) {
            _base_page = page;
        }
        public void SetLanguage() {
            _language = RewriteContext.Current.Params["lg"];
            if (string.IsNullOrEmpty(_language)) {
                _language = SiteManager.GetDefaultLanguage();
            }
        }
        public void SetLanguage(string lang) {
            _language = lang;
        }
        public void SetWebPage() {
            _page = WebPage.GetCurrentPage();
        }

        public void SetClub() {
            if (SiteContext.Current.User != null) {
                AspnetUser user = AspnetUser.FetchByID((Guid)SiteContext.Current.User.ProviderUserKey);
                _club = VwClubProfile.FetchByID((int)user.AspnetUserProfileRecords[0].EntityId);
            }
        }
        public void SetClub(int id) {
            _club = VwClubProfile.FetchByID(id);
        }

        #endregion

        public string HttpStatus { get { return _httpStatus; } set { _httpStatus = value; } }
        public int HttpStatusCode { get { return _httpStatusCode; } set { _httpStatusCode = value; } }


        #region ISiteContext Members

        #endregion
    }
}
